﻿using Client.Framework.Message;
using System.Collections.Generic;

namespace Client.Framework.Event
{
    /// <summary>
    /// 消息事件委托
    /// </summary>
    /// <param name="message">消息体</param>
    delegate void MessageDelegate(ResponseBaseMessage message);

    /// <summary>
    /// Author: Leil
    /// Description: 消息事件发布器
    /// CreateTime: 2020/5/12 11:13:25
    /// </summary>
    class MessageEventPublisher
    {
        private static readonly Dictionary<int, MessageDelegate> messageEventDic = new Dictionary<int, MessageDelegate>();

        /// <summary>
        /// 订阅事件
        /// </summary>
        /// <param name="msgId">消息号</param>
        /// <param name="messageDelegate">事件委托</param>
        public static void SubscribeEvent(int msgId, MessageDelegate messageDelegate)
        {
            if (!messageEventDic.ContainsKey(msgId))
            {
                messageEventDic.Add(msgId, messageDelegate);
                return;
            }
            messageEventDic[msgId] += messageDelegate;
        }

        /// <summary>
        /// 触发事件
        /// </summary>
        /// <param name="message">消息</param>
        public static void TriggerEvent(ResponseBaseMessage message)
        {
            int msgId = message.MsgId;
            if (messageEventDic.ContainsKey(msgId))
            {
                messageEventDic[msgId].Invoke(message);
            }
        }

    }
}
